/**
 * Created by Administrator on 2016/11/10.
 */
    var myApp=angular.module('MyApp',[]);

    myApp.controller('music',['$scope','$http',function($scope,$http){
    $scope.playing=false;
    $scope.count=0;
    $scope.audio=document.createElement('audio');
    $http.get('../json/music.json').success(function(response){
    $scope.srcs=response.srcS;
    $scope.audio.src=$scope.srcs[0].src;
    console.log($scope.audio.src);
//            for(var i=0;i<$scope.audio.srcs.length;i++){
//                $scope.audio.src=$scope.audio.srcs[i];
//            }
});
    $scope.play=function(){
    $scope.audio.play();

    $scope.playing=true;
};
    $scope.stop=function(){
    $scope.audio.pause();
    $scope.playing=false;
};
    function autoPlay(){
    $scope.audio.pause();
    $scope.playing=false;
    if($scope.count >= $scope.srcs.length){
    $scope.audio.src=$scope.srcs[0].src;
    $scope.count=0;
}else if($scope.count<0){
    $scope.audio.src=$scope.srcs[4].src;
    $scope.count=4;
}else{
    $scope.audio.src=$scope.srcs[$scope.count].src;
}

    console.log($scope.count);
    $scope.audio.play();
    $scope.playing=true;
}
    $scope.next=function(){
    ++$scope.count;
    autoPlay();
};
    $scope.before=function(){
    --$scope.count;
    autoPlay();
};
    $scope.audio.addEventListener('ended',function(){
    $scope.$apply(function(){
    $scope.stop();

});
    $scope.next();
})
}]);

    myApp.controller('Check',['$scope','$http',function($scope,$http){
    var ImgBox=$('#security-image');

    $scope.getImage = function(){//读取json文件
        $http.get('../json/security-image.json').success(function(response){
            $scope.srcs=response.securityImg;
            $scope.count=parseInt(Math.random()*($scope.srcs.length)+0);//生成一个随机0~img长度之间的随机数；
            ImgBox.attr('src',$scope.srcs[$scope.count].src);
            $scope.answer=$scope.srcs[$scope.count].answer;
            console.log($scope.answer);
        });
    };
    $scope.refresh=function(){//点击刷新验证码
     next();
    };
    function next(){
    $scope.getImage();
    }
    $scope.passwordNumber='';
    $scope.checkAnswer=function (){//判断验证码
        if($scope.passwordNumber.length >= $scope.answer.length && $scope.answer != $scope.passwordNumber){
           return false;
        }else if($scope.answer == $scope.passwordNumber){
            console.log('true');
          return true;
        }
    };
/********************************************************************/
    $scope.password='';
    $scope.passwordCheck='';
    var pwChenckBtn=$('#password-check');
    pwChenckBtn.on('keyup',function(){
        if($scope.passwordCheck.length >= $scope.password.length && $scope.password != $scope.passwordCheck){
            $(this).next().html('<i>'+'</i>'+'密码必须匹配。').addClass('span-change');
            return false;
        }else if($scope.password == $scope.passwordCheck){
            $(this).next().html('');
            return true;
        }
    });
    $scope.email='';
    $scope.emailCheck='';
    var emailCheck=$('#email-check');
    emailCheck.on('keyup',function(){
    if($scope.emailCheck.length >= $scope.email.length && $scope.email != $scope.emailCheck){
        $(this).next().html('<i>'+'</i>'+'邮箱必须匹配。').addClass('span-change');
        return false;
    }else if($scope.email == $scope.emailCheck){
        $(this).next().html('');
        return true;
    }
    });



}]);
